package com.blacktea.behavioralpatterns.visitorpattern;

import java.math.BigDecimal;

/**
 * @description: 订单价格访问者类
 * @author: black tea
 * @date: 2023/3/26 22:44
 */
public class OrderPriceVisitor implements OrderVisitor {

    private BigDecimal totalPrice = BigDecimal.ZERO;

    @Override
    public void visit(ProductOrderItem item) {
        totalPrice = totalPrice.add(
                item.getPrice()
                .multiply(new BigDecimal(item.getQuantity()))
        );
    }

    @Override
    public void visit(DiscountOrderItem item) {
        BigDecimal divide = item.getDiscount().divide(new BigDecimal("100"), 2, BigDecimal.ROUND_DOWN);
        totalPrice = totalPrice.multiply(divide);
        totalPrice = totalPrice.setScale(2, BigDecimal.ROUND_DOWN);
    }

    @Override
    public BigDecimal getResult() {
        return totalPrice;
    }
}
